import React from 'react';
import getT from '../../lib/getT';
import SearchBar from '../SearchBar';
import ColumnSet from '../ColumnSet';
const COLUMN_SET = 'columnSet';
const SEARCH = 'search';
const innerGrids = [{
    key: COLUMN_SET,
    order: 999,
}, {
    key: SEARCH,
    order: 9
}];
/* 
* grids结构说明
* grid.order: 顺序号，必须是整数
* grid.render: 组件渲染方法
* grid.key:grid唯一编码
*/
function useGenGrids(params) {
    const { refreshTable, customToolBars = [], hasSearch, hasColumnSet, columnsInPanel, updateColumnsInPanel, updateColumnsDisplay } = params;
    // * 判断columnSet和search的工具按钮是否生效
    let grids = [...customToolBars, ...innerGrids].filter(v => {
        if (v.key === COLUMN_SET) {
            return hasColumnSet;
        } else if (v.key === SEARCH) {
            return hasSearch;
        } else {
            return true;
        }
    });
    // * 设置columnSet和search的render方法
    grids.forEach(grid => {
        if (grid.key === COLUMN_SET) {
            grid.render = () => {
                return (
                    <ColumnSet
                        dataSource={columnsInPanel}
                        name={getT("列表显示")}
                        onConfigChange={(panels) => {
                            updateColumnsInPanel(panels);
                            updateColumnsDisplay(panels);
                        }}
                        onEnd={(panels) => {
                            updateColumnsInPanel(panels);
                            updateColumnsDisplay(panels);
                        }}
                    ></ColumnSet>
                );
            }
        } else if (grid.key === SEARCH) {
            grid.render = () => {
                return (
                    <SearchBar
                        onSearch={refreshTable}
                    />
                );
            }
        }
    });
    // * toolbar按钮排序，如果order非整数，则替换为默认次序0
    grids.sort((a, b) => {
        return (Number.isInteger(a.order) ? a.order : 0) - (Number.isInteger(b.order) ? b.order : 0);
    });
    return grids;
}
export default useGenGrids;